{% import "external.j2" as external %}
{# #}
{# Execute the caller-supplied macro with the value of a potentially-nested
 # attribute/key as the argument, if it can be reached (all keys/attributes
 # exist). The container is passed as the first argument, and the keys as the
 # variable number of following arguments.
 #
 # E.g. this:
 #  {% call(value) with(data, "attr", "k e y", "000", "bar" %}
 #    "{{value}}"
 #  {% endcall %}
 #
 # Would be equivalent to:
 #  {% if data.attr is defined and
 #        "k e y" in data.attr and
 #        "000" in data.attr["k e y"] and
 #        data.attr["k e y"]["000"].bar is defined %}
 #    "{{ data.attr["k e y"]["000"].bar }}"
 #  {% endif %}
 #}
{%- macro with(container) -%}
  {%- set with_ns = namespace(value=container) -%}
  {%- for key in varargs -%}
    {%- if with_ns.value != with_ns -%}
      {%- if with_ns.value[key] is defined -%}
        {%- set with_ns.value = with_ns.value[key] -%}
      {%- else -%}
        {# Use a unique value to stop resolution #}
        {%- set with_ns.value = with_ns -%}
      {%- endif -%}
    {%- endif -%}
  {%- endfor -%}
  {%- if with_ns.value != with_ns -%}
    {{- caller(with_ns.value) -}}
  {%- endif -%}
{%- endmacro -%}
{# #}
{% macro test_tasks(id, test) -%}
  {% if VARIABLES.coverage %}
    <!--generate base .info coverage data labeled TEST_NAME-->
    <task name="/kernel/kcov/start" role="STANDALONE">
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#kcov/start"/>
      <params>
        <param name="TEST_NAME" value="{{ test.name | e }}"/>
        <param name="CKI_IS_TEST" value="False"/>
      </params>
    </task>
  {% endif %}
  <task name="{{ test.name }}" role="{{ test.role }}">
    {% if test.origin == "kernel_public_tests" %}
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#{{ test.location }}"/>
    {% elif test.origin == "kernel_internal_tests" %}
      <fetch url="{{ VARIABLES.kernel_internal_tests_url }}#{{ test.location }}"/>
    {% elif test.origin == "desktopqe_kernel_drm_rhel8" %}
      <fetch url="{{ VARIABLES.desktopqe_kernel_drm_rhel8_url }}#{{ test.location }}"/>
    {% elif test.origin == "desktopqe_kernel_drm_rhel9" %}
      <fetch url="{{ VARIABLES.desktopqe_kernel_drm_rhel9_url }}#{{ test.location }}"/>
    {% elif test.origin == "desktopqe_kernel_drm_rhel10" %}
      <fetch url="{{ VARIABLES.desktopqe_kernel_drm_rhel10_url }}#{{ test.location }}"/>
    {% elif test.origin == "desktopqe_mesa_rhel8" %}
      <fetch url="{{ VARIABLES.desktopqe_mesa_rhel8_url }}#{{ test.location }}"/>
    {% elif test.origin == "desktopqe_mesa_rhel9" %}
      <fetch url="{{ VARIABLES.desktopqe_mesa_rhel9_url }}#{{ test.location }}"/>
    {% elif test.origin == "desktopqe_mesa_rhel10" %}
      <fetch url="{{ VARIABLES.desktopqe_mesa_rhel10_url }}#{{ test.location }}"/>
    {% elif test.origin == "ofa_repo" %}
      <fetch url="{{ VARIABLES.ofa_repo_url }}#{{ test.location }}"/>
    {% endif %}
    <params>
      <param name="CKI_ID" value="{{ id }}"/>
      <param name="CKI_IS_TEST" value="True"/>
      <param name="CKI_NAME" value="{{ test.name | e }}"/>
      {% if test.universal_id is not none %}
        <param name="CKI_UNIVERSAL_ID" value="{{ test.universal_id | e }}"/>
      {% endif %}
      <param name="CKI_WAIVED" value="{{ test.waived }}"/>
      {% if test.maintainers %}
        {% set sep = joiner(", ") %}
        <param name="CKI_MAINTAINERS" value="
          {%- for m in test.maintainers -%}
            {{- sep() -}}
            {{- "%s <%s>" | format(m.name, m.email) | e -}}
            {%- if 'gitlab' in m -%}
              {{- (" / " + m.gitlab) | e -}}
            {%- endif -%}
          {%- endfor -%}
        "/>
      {% endif %}
      <param name="CKI_SELFTESTS_URL" value="{{ VARIABLES.selftests_url }}"/>
      <param name="KILLTIMEOVERRIDE" value="{{ test.max_duration_seconds }}"/>
      {% call(id) with(external.tests, test.origin, test.location, "id") %}
        <param name="CKI_POLARION_ID" value="{{ id }}"/>
      {% endcall %}
      {% for name, value in test.environment.items() %}
        <param name="{{ name }}" value="{{ value }}"/>
      {% endfor %}
    </params>
  </task>
  {% if VARIABLES.coverage %}
    <!--generate end .info and merge with base .info of TEST_NAME-->
    <task name="/kernel/kcov/end" role="STANDALONE">
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#kcov/end"/>
      <params>
        <param name="TEST_NAME" value="{{ test.name | e }}"/>
        <param name="CKI_IS_TEST" value="False"/>
      </params>
    </task>
  {% endif %}
{% endmacro %}
{# #}
{% macro host_condition(scene, condition,
                        parent_and_op=True, and_op=True) %}
  {% if condition is string %}
    {% if condition.startswith("T:") %}
      {% set ARCH = scene.arch %}
      {% set TREE = scene.tree %}
      {% set COMPONENTS = scene.components %}
      {%- include condition[2:] -%}
    {% elif condition.startswith("H:") %}
      <hostname op="=" value="{{ condition[2:] }}"/>
    {% else %}
      {{ "Invalid string condition" / 0 }}
    {% endif %}
  {% elif condition is mapping %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "<and>" if and_op else "<or>" }}
    {% endif %}
    {% for k, v in condition.items() %}
      {% if k == "not" %}
        <not>{{ host_condition(scene=scene, condition=v) }}</not>
      {% elif k == "or" %}
        {{ host_condition(scene=scene, condition=v,
                          parent_and_op=and_op, and_op=False) }}
      {% elif k == "and" %}
        {{ host_condition(scene=scene, condition=v,
                          parent_and_op=and_op, and_op=True) }}
      {% elif k == "templates" %}
        {{ host_condition(scene=scene, condition=v,
                          parent_and_op=and_op) }}
      {% elif k == "hostnames" %}
        {{ host_condition(scene=scene, condition=v,
                          parent_and_op=and_op) }}
      {% else %}
        {{ "Invalid host condition key" / 0 }}
      {% endif %}
    {% endfor %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "</and>" if and_op else "</or>" }}
    {% endif %}
  {% elif condition is sequence %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "<and>" if and_op else "<or>" }}
    {% endif %}
    {% for v in condition %}
      {{ host_condition(scene=scene, condition=v,
                        parent_and_op=and_op) }}
    {% endfor %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "</and>" if and_op else "</or>" }}
    {% endif %}
  {% else %}
    {{ "Invalid host condition type" / 0 }}
  {% endif %}
{% endmacro %}
{# #}
{% macro recipe(host,
                scene,
                tests_ns,
                distro_requires="",
                host_requires="",
                kernel_options="",
                kernel_options_post="",
                ks_append="",
                ks_meta="",
                repos="") %}
  {% set TREE = scene.tree %}
  {% set ARCH = scene.arch %}
  {% set COMPONENTS = scene.components %}
  <!-- host type: {{ host.type_name }} -->
  {# NOTE(rasibley): Omits the fedora-updates repo for Fedora, see bz 1734557 #}
  <recipe
    ks_meta="
      {%- if ks_meta -%}
        {{- ks_meta + " " -}}
      {%- endif -%}
      {%- if not TREE.startswith("rhel6") -%}
        {{- "redhat_ca_cert " -}}
      {%- endif -%}
      {{- "harness='restraint-rhts beakerlib' " -}}
      {# Rawhide uses btrfs by default, for ELN we want to test a FS that will be used by RHEL #}
      {%- if TREE == "eln" -%}
        {{- "fstype=xfs " -}}
      {%- endif -%}
      {# https://issues.redhat.com/browse/RHEL-35250 #}
      {%- if TREE.startswith("rhel10") or TREE.startswith("c10s") -%}
        {{- "method=http " -}}
      {%- endif -%}
      {# Brew builds get the full SELinux treatment jic any of the avc denials actually break something important #}
      {%- if not TREE.startswith("rhel") or "officialbuild" not in COMPONENTS -%}
        {# Set selinux to permissive mode to get all avc denials in one fell swoop; except automotive #}
        {{- "selinux=--permissive" -}}
      {%- endif -%}
    "
    kernel_options="{{ kernel_options }}"
    kernel_options_post="{{ kernel_options_post }}"
  >
    <distroRequires>
      <distro_arch op="=" value="{{ ARCH }}"/>
      {{- distro_requires -}}
    </distroRequires>
    {% if VARIABLES.hostname %}
      <hostRequires>
        <hostname op="like" value="{{ VARIABLES.hostname }}"/>
      </hostRequires>
    {% elif host['hostname'] %}
      <hostRequires>
        <hostname op="=" value="{{ host['hostname'] }}"/>
      </hostRequires>
    {% else %}
      <hostRequires>
        {% for bus in VARIABLES.device_buses.split(',') if VARIABLES.device_buses %}
          <device op="=" bus="{{ bus }}" />
        {% endfor %}
        {% for driver in VARIABLES.device_drivers.split(',') if VARIABLES.device_drivers %}
            <device op="=" driver="{{ driver }}" />
        {% endfor %}
        {% for type in VARIABLES.device_types.split(',') if VARIABLES.device_types %}
            <device op="=" type="{{ type }}" />
        {% endfor %}
        <arch op="=" value="{{ ARCH }}"/>
        {% if ARCH == "x86_64" %}
          <not>
            <or>
              {# NOTE(mhayden): Exclude XCI machines from test jobs since they #}
              {# are not actively maintained by anyone. #}
              <hostname op="like" value="xci%"/>
              <hostname op="=" value="c101.xci.lab.eng.rdu2.redhat.com"/>
              <hostname op="=" value="c106.xci.lab.eng.rdu2.redhat.com"/>
              {# dell-pe-fm120-2b is an old machine and has HW problems #}
              {# https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/issues/699 #}
              <hostname op="=" value="dell-pe-fm120-2b.dell2.lab.eng.bos.redhat.com"/>
              {# dell-pe-fm120-1c is an old machine and has HW problems #}
              {# https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/issues/702 #}
              <hostname op="=" value="dell-pe-fm120-1c.dell2.lab.eng.bos.redhat.com"/>
              {# dell-pe-fm120-2d is an old machine and has HW problems #}
              {# https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/issues/736 #}
              <hostname op="=" value="dell-pe-fm120-2d.dell2.lab.eng.bos.redhat.com"/>
              {# These machines don't have console log working #}
              {# see SN: PNT1161789 #}
              <hostname op="like" value="cloud-qe%"/>
              {# These machines have performance issues #}
              {# https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/issues/1395 #}
              <hostname op="like" value="lenovo-rd230-%.khw2.lab.eng.bos.redhat.com"/>
            </or>
          </not>
        {% endif %}
        {% if ARCH == "aarch64" %}
          <not>
            <or>
              <hostname op="like" value="hpe-comanche%"/>
              <hostname op="like" value="dell-venom%"/>
              {# aarch64-hv01 VMs seem to have extremely slow IO performance #}
              <hostname op="like" value="aarch64-hv01%"/>
              {# disable until SNOW#RITM1942603 is fixed #}
              <hostname op="=" value="ampere-hr330a-04.lab.eng.rdu2.redhat.com"/>
            </or>
          </not>
        {% endif %}
        {% if ARCH == "s390x" %}
          <not>
            <or>
              {# NOTE(bgoncav): provisioning of these kernelci machines are not reliable #}
              <hostname op="like" value="kernelci-%.s390.bos.redhat.com"/>
            </or>
          </not>
        {% endif %}
        {# kernel-rt should be allowed to use other controllers like pek2: #}
        {# https://gitlab.com/redhat/centos-stream/tests/kernel/kpet-db/-/merge_requests/834#note_1212616261 #}
        {% if (ARCH != "s390x") and ("rt" not in COMPONENTS) %}
          <or>
            <labcontroller op="=" value="lab-02.hosts.prod.psi.bos.redhat.com"/>
            <labcontroller op="=" value="lab-01.beak-001.lab.bos2.dc.redhat.com"/>
            <labcontroller op="=" value="lab-02.rhts.eng.rdu.redhat.com"/>
            <labcontroller op="=" value="lab-02.rhts.eng.brq.redhat.com"/>
            <labcontroller op="=" value="beaker.ofa.iol.unh.edu"/>
          </or>
        {% endif %}
        {{ host_condition(scene=scene, condition=host.condition) }}
        {{- host_requires -}}
      </hostRequires>
    {% endif %}
    <repos>
      {{- repos -}}
    </repos>
    <partitions>
      {%- for template in host.partitions_list -%}
        {%- include template -%}
      {%- endfor -%}
    </partitions>
    <ks_appends>
      <ks_append>
        {%- filter unindent %}
          <![CDATA[
          # try to make the machine time sane, part 1
          timezone UTC

          %post
          set -x
          sed -i 's/^#DefaultTasksMax.*/DefaultTasksMax=50%/' /etc/systemd/system.conf

          # try to make the machine time sane, part 2
          type -p chronyc && chronyc makestep

          {# NOTE(bgoncalv): Add RHEL6 Red Hat CA Cert #}
          {% if TREE.startswith("rhel6") %}
            {% filter unindent %}
              curl -L --retry 20 --remote-time -o /etc/pki/ca-trust/source/anchors/Current-IT-Root-CAs.pem https://certs.corp.redhat.com/certs/Current-IT-Root-CAs.pem
              update-ca-trust enable
              update-ca-trust extract
            {% endfilter %}
          {% endif %}

          {% if "rt" in COMPONENTS %}
            {% filter unindent %}
              sed -i '/^isolated_cores/d' /etc/tuned/realtime-variables.conf
              echo "isolated_cores=" >> /etc/tuned/realtime-variables.conf
            {% endfilter %}
          {% endif %}

          # Workaround for beaker not configuring sources repo, see: RITM1098030
          {% filter unindent %}
            if [[ -f /etc/yum.repos.d/beaker-BaseOS-debuginfo.repo ]]; then
              # For the test we need to download sources, it looks like only BaseOS is enough
              cp /etc/yum.repos.d/beaker-BaseOS-debuginfo.repo /etc/yum.repos.d/cki-BaseOS-source.repo
              sed -i 's/debuginfo/source/' /etc/yum.repos.d/cki-BaseOS-source.repo
              sed -i 's/{{ ARCH }}\/debug/source/' /etc/yum.repos.d/cki-BaseOS-source.repo
            fi
          {% endfilter %}

          # some tests like xfstests and lvm device-mapper create partitions in beaker
          # the tests might change the devices under these partition and that is fine.
          # make sure this doesn't block a machine from booting if the devices are invalid.
          sed -Ei 's/(.*\/mnt\/xfstests\/mnt.* xfs) .*/\1 defaults,nofail 0 0/g' /etc/fstab
          sed -Ei 's/(.*\/mnt\/dmtest\/.* xfs) .*/\1 defaults,nofail 0 0/g' /etc/fstab

          %end
          ]]>
        {%- endfilter -%}
      </ks_append>
      {%- for template in host.kickstart_list -%}
        {%- include template -%}
      {%- endfor -%}
      {{- ks_append -}}
    </ks_appends>
    {% if host.ignore_panic %}
      <watchdog panic="ignore"/>
    {% endif %}
    {% if "64kpages" in COMPONENTS %}
      <packages>
        <package name="kernel-64k"/>
        <package name="-kernel"/>
        <package name="-kvdo"/>
        <package name="-kmod-kvdo"/>
      </packages>
    {% endif %}
    {% if TREE not in ("autosd", "rhivos") %}
      <task name="CKI/restraint" role="None">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#cki/restraint"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          <param name="CKI_IS_TEST" value="True"/>
          <param name="CKI_NAME" value="CKI/restraint"/>
          <param name="CKI_UNIVERSAL_ID" value="redhat_restraint"/>
          <param name="CKI_WAIVED" value="True"/>
        </params>
      </task>
      {% set tests_ns.id = tests_ns.id + 1 %}
    {% endif %}
    {% if TREE in ("autosd", "rhivos") %}
      <task name="Validate automotive booted OS" role="None">
        <!-- Automotive testing provision image validation-->
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/verify-automotive-img"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          {% set tests_ns.id = tests_ns.id + 1 %}
          <param name="CKI_IS_TEST" value="True"/>
          <param name="CKI_NAME" value="Validate Booted OS Release"/>
          <param name="CKI_UNIVERSAL_ID" value="automotive_validate_os_release"/>
          <param name="CKI_WAIVED" value="False"/>
          <param name="REQUESTED_RELEASE" value="{{ VARIABLES.automotive_config_release }}"/>
        </params>
      </task>
      <task name="/distribution/command : clean_tmt" role="STANDALONE">
        <!-- Automotive hosts provisioned w/TF will have some tmt local scripts which interfere with restraint-->
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/command"/>
        <params>
          <param name="TESTARGS" value="patterns=('tmt*' 'rhts*' 'rstrnt*'); for pattern in
          &quot;${patterns[@]}&quot;; do rm -r /usr/local/bin/$pattern &gt;/dev/null 2&gt;&amp;1 &amp;&amp;
          echo &quot;removed $pattern in /usr/local/bin&quot;; done || true"/>
          <param name="CKI_IS_TEST" value="False"/>
        </params>
      </task>
      <task name="/distribution/command : root CA" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/command"/>
        <params>
          <param name="TESTARGS" value="curl -L --retry 3 --remote-time -o /etc/pki/ca-trust/source/anchors/Current-IT-Root-CAs.pem
          https://certs.corp.redhat.com/certs/Current-IT-Root-CAs.pem
          &amp;&amp; update-ca-trust enable
          &amp;&amp; update-ca-trust extract"/>
          <param name="CKI_IS_TEST" value="False"/>
        </params>
      </task>
      {% if TREE == "rhivos" %}
        <!-- RHIVOS testing needs to add repos after image build-->
        <task name="Automotive Repo Setup" role="None">
          <fetch url="{{ VARIABLES.rhivos_repo_url }}#scripts"/>
          <params>
            <param name="CKI_ID" value="{{ tests_ns.id }}"/>
            <param name="CKI_IS_TEST" value="True"/>
            <param name="CKI_NAME" value="Automotive Custom Repo Setup"/>
            <param name="CKI_UNIVERSAL_ID" value="cki_automotive_repos"/>
            <param name="CKI_WAIVED" value="False"/>
            <param name="KILLTIMEOVERRIDE" value="1400"/>
          </params>
        </task>
        {% set tests_ns.id = tests_ns.id + 1 %}
      {% endif %}
      <!-- RHIVOS testing needs to install tracing systemd service-->
      <task name="Automotive install systemd tracing service" role="None">
        <fetch url="{{ VARIABLES.rhivos_repo_url }}#scripts/restraint_wrapper/install_systemd_tracing"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          {% set tests_ns.id = tests_ns.id + 1 %}
          <param name="CKI_IS_TEST" value="True"/>
          <param name="CKI_NAME" value="Automotive systemd tracing service"/>
          <param name="CKI_UNIVERSAL_ID" value="automotive_systemd_tracing"/>
          <param name="CKI_WAIVED" value="False"/>
          <param name="KILLTIMEOVERRIDE" value="1400"/>
        </params>
      </task>
    {% endif %}
    <task name="SELinux Custom Module Setup" role="None">
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/selinux-custom-modules"/>
      <params>
        <param name="CKI_ID" value="{{ tests_ns.id }}"/>
        <param name="CKI_IS_TEST" value="True"/>
        <param name="CKI_NAME" value="SELinux Custom Module Setup"/>
        <param name="CKI_UNIVERSAL_ID" value="selinux_custom_module_setup"/>
        <param name="CKI_WAIVED" value="True"/>
      </params>
    </task>
    {% set tests_ns.id = tests_ns.id + 1 %}
    {% if TREE not in ("autosd", "rhivos") %}
    {# check-install requires restraint-rhts that is not available on automotive images #}
        <task name="/distribution/check-install" role="STANDALONE">
          <fetch url="https://github.com/beaker-project/beaker-core-tasks/archive/master.tar.gz#check-install"/>
          <params>
            <param name="CKI_IS_TEST" value="False"/>
          </params>
        </task>
    {% endif %}
    {# NOTE(rasibley): upstream jobs require latest kmod #}
    {% if TREE == "upstream" %}
      <task name="/distribution/command" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/command"/>
        <params>
          <param name="TESTARGS" value="dnf update -y kmod --enablerepo=updates-testing"/>
          <param name="CKI_IS_TEST" value="False"/>
        </params>
      </task>
    {% endif %}
    {# NOTE(mhayden): Workaround for ppc64le + 4.20. BZ 1647947. #}
    {% if ARCH == "ppc64le" and not TREE.startswith("rhel7") %}
      <task name="/distribution/command" role="None">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/command"/>
        <params>
          <param name="CMDS_TO_RUN" value="echo 'net.core.bpf_jit_limit = 262144000' | tee -a /etc/sysctl.d/ppc64le-bpf-jit-fix.conf"/>
          <param name="CKI_IS_TEST" value="False"/>
        </params>
      </task>
    {% endif %}
    <task name="/test/misc/machineinfo">
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#test/misc/machineinfo"/>
      <params>
        <param name="CKI_ID" value="{{ tests_ns.id }}"/>
        <param name="CKI_IS_TEST" value="True"/>
        <param name="CKI_NAME" value="machineinfo"/>
        <param name="CKI_UNIVERSAL_ID" value="machineinfo"/>
        {# Failures in this task should never block pipelines #}
        <param name="CKI_WAIVED" value="True"/>
      </params>
    </task>
    {% set tests_ns.id = tests_ns.id + 1 %}
    {% if host.preboot_tasks %}
      {% include host.preboot_tasks %}
    {% endif %}
    {% if VARIABLES.brew_build_nvr %}
      <task name="distribution/brew-pkginstall" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/brew-pkginstall"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          <param name="CKI_IS_TEST" value="True"/>
          <param name="CKI_NAME" value="distribution/brew-pkginstall"/>
          <param name="CKI_UNIVERSAL_ID" value="redhat_brew_pkginstall"/>
          <param name="CKI_MAINTAINERS" value="Bruno Goncalves &lt;bgoncalv@redhat.com&gt; / bgoncalv"/>
          <param name="BUILD_NVR" value="{{ VARIABLES.brew_build_nvr }}"/>
          <param name="KILLTIMEOVERRIDE" value="1800"/>
          <param name="CKI_WAIVED" value="False"/>
          {# Abort the recipe in case of LWD #}
          {# Used at: https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/blob/main/cki/restraint/runtest.sh #}
          <param name="CKI_ABORT_RECIPE_ON_LWD" value="1"/>
        </params>
      </task>
      {% set tests_ns.id = tests_ns.id + 1 %}
    {% endif %}
    {% if VARIABLES.brew_kernel_name and VARIABLES.brew_kernel_variant and VARIABLES.brew_kernel_version %}
      <task name="distribution/kernelinstall" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/kernelinstall"/>
        <params>
          <param name="CHECK_CALLTRACE" value="1"/>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          <param name="CKI_IS_TEST" value="True"/>
          <param name="CKI_NAME" value="distribution/kernelinstall"/>
          <param name="CKI_UNIVERSAL_ID" value="redhat_kernelinstall"/>
          <param name="CKI_MAINTAINERS" value="Bruno Goncalves &lt;bgoncalv@redhat.com&gt; / bgoncalv"/>
          <param name="CKI_WAIVED" value="False"/>
          <param name="KERNELARGEXTRAMODULES" value="1"/>
          <param name="KERNELARGINTERNALMODULES" value="1"/>
          <param name="KERNELARGNAME" value="{{ VARIABLES.brew_kernel_name }}"/>
          <param name="KERNELARGVARIANT" value="{{ VARIABLES.brew_kernel_variant }}"/>
          <param name="KERNELARGVERSION" value="{{ VARIABLES.brew_kernel_version }}"/>
          <param name="KILLTIMEOVERRIDE" value="1800"/>
          {# Abort the recipe in case of LWD #}
          {# Used at: https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/blob/main/cki/restraint/runtest.sh #}
          <param name="CKI_ABORT_RECIPE_ON_LWD" value="1"/>
        </params>
      </task>
      {% set tests_ns.id = tests_ns.id + 1 %}
    {% endif %}
    {% if scene.kernel is not none %}
      {% set BOOT_TEST_TIMEOUT=5400 %}
      {% set BOOT_TEST_MAINTAINER="Bruno Goncalves &lt;bgoncalv@redhat.com&gt; / bgoncalv" %}
      {% if TREE in ("autosd", "rhivos") %}
        {# the boot test takes very long time on automotive debug kernels #}
        {% set BOOT_TEST_TIMEOUT=9000 %}
        {% set BOOT_TEST_MAINTAINER="Brian Grech &lt;bgrech@redhat.com&gt; / bgrech" %}
        <task name="Install Dependencies rpm-ostree">
          <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/preinstall-test-packages"/>
          <params>
            <param name="CKI_ID" value="{{ tests_ns.id }}"/>
            <param name="CKI_IS_TEST" value="True"/>
            <param name="CKI_NAME" value="rpmostree-dep-install"/>
            <param name="CKI_UNIVERSAL_ID" value="dep-install"/>
            <param name="BEAKERXML_URL" value="{{ VARIABLES.beaker_xml }}"/>
            {# Failures in this task should never block pipelines #}
            <param name="CKI_WAIVED" value="True"/>
          </params>
        </task>
      {% endif %}
      <task name="Boot test" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/kpkginstall"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          <param name="CKI_IS_TEST" value="True"/>
          <param name="CKI_NAME" value="Boot test"/>
          <param name="CKI_UNIVERSAL_ID" value="boot"/>
          <param name="CKI_MAINTAINERS" value="{{ BOOT_TEST_MAINTAINER }}"/>
          <param name="KPKG_URL" value="{{ scene.kernel }}"/>
          <param name="KILLTIMEOVERRIDE" value="{{ BOOT_TEST_TIMEOUT }}"/>
          <param name="CKI_WAIVED" value="False"/>
          {# Abort the recipe in case of LWD #}
          {# Used at: https://gitlab.com/redhat/centos-stream/tests/kernel/kernel-tests/-/blob/main/cki/restraint/runtest.sh #}
          <param name="CKI_ABORT_RECIPE_ON_LWD" value="1"/>
        </params>
      </task>
      {% set tests_ns.id = tests_ns.id + 1 %}
    {% endif %}
    {% if VARIABLES.coverage %}
      <task name="/kernel/kcov/prepare" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#kcov/prepare"/>
        <params>
          <!--don't log .info file for every test to decrease size of artifacts-->
          <param name="ONLY_FINAL_INFO" value="true"/>
          {% if VARIABLES.coverage_dirs %}
              <param name="KDIR" value="{{ VARIABLES.coverage_dirs }}"/>
          {% endif %}
          <param name="CKI_IS_TEST" value="False"/>
        </params>
      </task>
    {% endif %}
    {% if host.postboot_tasks %}
      {% include host.postboot_tasks %}
    {% endif %}
    {% for test in host.tests %}
      {{- test_tasks(tests_ns.id, test) -}}
      {% set tests_ns.id = tests_ns.id + 1 %}
    {% endfor %}
    {% if VARIABLES.coverage %}
      <!--combine all coverage files for the recipe into one. Generate html tgz
          if this is the only recipe-->
      <task name="/kernel/kcov/finalize" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#kcov/finalize"/>
        <params>
          {# this task tries to upload large file, this can take long time #}
          <param  name="KILLTIMEOVERRIDE" value="{{ 5 * 3600 }}"/>
          <param name="CKI_IS_TEST" value="False"/>
        </params>
      </task>
    {% endif %}
    {% set REBOOT_TEST_MAINTAINER="Bruno Goncalves &lt;bgoncalv@redhat.com&gt; / bgoncalv" %}
    {% if TREE in ("autosd", "rhivos") %}
      {% set REBOOT_TEST_MAINTAINER="Brian Grech &lt;bgrech@redhat.com&gt; / bgrech" %}
    {% endif %}
    {# reboot test should run as last test task #}
    {# workaround for https://gitlab.com/cki-project/upt/-/issues/63 #}
    <task name="Reboot test" role="STANDALONE">
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#misc/reboot-test"/>
      <params>
        <param name="CKI_ID" value="{{ tests_ns.id }}"/>
        <param name="CKI_IS_TEST" value="True"/>
        {% set tests_ns.id = tests_ns.id + 1 %}
        <param name="CKI_NAME" value="Reboot test"/>
        <param name="CKI_UNIVERSAL_ID" value="reboot_test"/>
        <param name="CKI_MAINTAINERS" value="{{ REBOOT_TEST_MAINTAINER }}"/>
        <param name="KILLTIMEOVERRIDE" value="3600"/>
        <param name="CKI_WAIVED" value="False"/>
      </params>
    </task>
    {% if TREE in ("autosd", "rhivos") %}
      <!-- Automotive hosts trace result tasks-->
      <task name="Check stack trace results" role="STANDALONE">
        <fetch url="{{ VARIABLES.kernel_public_tests_url }}#memory/mmra/validate_kernel_stack_max_size"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          <param name="CKI_IS_TEST" value="True"/>
          {% set tests_ns.id = tests_ns.id + 1 %}
          <param name="CKI_NAME" value="check stack trace results"/>
          <param name="CKI_UNIVERSAL_ID" value="ck_stack_trace_results"/>
          <param name="CKI_MAINTAINERS" value="mm-qe &lt;mm-qe@redhat.com&gt; / mm-qe"/>
          <param name="KILLTIMEOVERRIDE" value="1400"/>
          <param name="CKI_WAIVED" value="False"/>
        </params>
      </task>
      <task name="Check stack timerlat trace results" role="None">
        <fetch url="{{ VARIABLES.kernel_internal_tests_url }}#rt-tests/evaluate_timerlat_trace"/>
        <params>
          <param name="CKI_ID" value="{{ tests_ns.id }}"/>
          <param name="CKI_IS_TEST" value="True"/>
          {% set tests_ns.id = tests_ns.id + 1 %}
          <param name="CKI_NAME" value="check stack timerlat trace results"/>
          <param name="CKI_UNIVERSAL_ID" value="ck_stack_timerlat_results"/>
          <param name="CKI_MAINTAINERS" value="Charles Shi &lt;cheshi@redhat.com&gt; / cheshi"/>
          <param name="CKI_WAIVED" value="False"/>
          <param name="KILLTIMEOVERRIDE" value="1400"/>
          <param name="MAX_TIMERLAT_LATENCY" value="86400000000"/>
        </params>
      </task>
    {% endif %}
    {# dummy task to be executed last #}
    {# https://gitlab.com/cki-project/upt/-/issues/56#note_908988653 #}
    <task name="/distribution/command" role="STANDALONE">
      <fetch url="{{ VARIABLES.kernel_public_tests_url }}#distribution/command"/>
      <params>
        <param name="TESTARGS" value=":"/>
        <param name="CKI_IS_TEST" value="False"/>
      </params>
    </task>
  </recipe>
{% endmacro %}
{# #}
<?xml version="1.0" encoding="UTF-8"?>
<job retention_tag="60days" group="{{ VARIABLES.job_group }}">
  <whiteboard>{{ DESCRIPTION }}</whiteboard>

  {#
    Collect recipesets and their scenes into a flat list, so that we can sort
    recipesets by their host types to maintain stable output order and
    simplify tracking both kpet and kpet-db changes.
  #}
  {# TODO Switch back to SCENE/recipeset iterating after changes are done #}
  {% set host_types_scene_recipesets = [] %}
  {% for scene in SCENES %}
    {% for recipeset in scene.recipesets %}
      {% set _ = host_types_scene_recipesets.append({
          "types": recipeset | map(attribute="type_name") | join("\n"),
          "scene": scene,
          "recipeset": recipeset
         })
      %}
    {% endfor %}
  {% endfor %}

  {# Start with global test ID = 1 #}
  {% set tests_ns = namespace(id=1) %}

  {# Build recipeSets of normal host types #}
  {% for host_types_scene_recipeset in
         host_types_scene_recipesets |
         sort(attribute="types", case_sensitive=True) %}
    {% set scene = host_types_scene_recipeset.scene %}
    {% set recipeset = host_types_scene_recipeset.recipeset %}
    {% set TREE = scene.tree %}
    {% set ARCH = scene.arch %}
    {% set COMPONENTS = scene.components %}
    {% import "trees/" + TREE + ".j2" as tree with context %}
    <recipeSet>
      {% for host in recipeset %}
        {{- recipe(host=host,
                   scene=scene,
                   tests_ns=tests_ns,
                   distro_requires=tree.distro_requires | default(""),
                   host_requires=tree.host_requires | default(""),
                   kernel_options=[
                      tree.kernel_options | default(""), host.kernel_options
                   ] | select | join(" "),
                   kernel_options_post=[
                      tree.kernel_options_post | default(""), host.kernel_options_post
                   ] | select | join(" "),
                   ks_append=tree.ks_append | default(""),
                   ks_meta=tree.ks_meta | default(""),
                   repos=tree.repos | default("")) -}}
      {% endfor %}
    </recipeSet>
  {% endfor %}
</job>
